package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.vo.CommmentPostBO;
import com.cskaoyan.bean.wx.vo.WxCommentCountVO;
import com.cskaoyan.bean.wx.vo.WxCommentListShowVO;
import com.cskaoyan.bean.wx.vo.WxCommentPageData;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.utils.MarketPageUtil;
import com.cskaoyan.utils.ShiroGetSubjectMessageUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public BasePageData selectComment(Integer userId, Integer valueId, BaseParam param) {

        PageHelper.startPage(param.getPage() , param.getLimit());
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId).andDeletedEqualTo(false);
        }

        criteria.andDeletedEqualTo(false);
        commentExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        return BasePageData.commentOk(comments);
    }

    @Override
    public void updateContent(Integer commentId, String content) {
        Comment comment1 = commentMapper.selectByPrimaryKey(commentId);
        if (comment1.getContent() != null){
            return;
        }

        comment1.setContent(content);
        comment1.setUpdateTime(new Date());
        commentMapper.updateByPrimaryKeySelective(comment1);
    }

    @Override
    public void deleteComment(Comment comment) {
        comment.setDeleted(true);
        comment.setUpdateTime(new Date());
        commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Override
    public WxCommentPageData queryCommentListByValueIdAndTypeAndshowType(String valueId, Integer type, Integer showType, BaseParam baseParam) {
        MarketPageUtil.startPage(baseParam);

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(Integer.parseInt(valueId)).andTypeEqualTo(type.byteValue());
        if(showType == 1) criteria.andHasPictureEqualTo(true);

        List<Comment> comments = commentMapper.selectByExample(commentExample);

        List<WxCommentListShowVO> wxCommentListShowVOS = new ArrayList<>();
        WxCommentPageData ok = WxCommentPageData.ok(comments);
        List<Comment> list = ok.getList();

        for (Comment comment : list) {
            Integer userId = comment.getUserId();
            User user = userMapper.selectByPrimaryKey(userId);

            WxCommentListShowVO wxCommentListShowVO = new WxCommentListShowVO();
            WxCommentListShowVO.UserInfo userInfo = wxCommentListShowVO.getUserInfo();
            userInfo.setAvatarUrl(user.getAvatar());
            userInfo.setNickname(user.getNickname());

            wxCommentListShowVO.setAddTime(comment.getAddTime());
            wxCommentListShowVO.setAdminContent(comment.getContent());
            wxCommentListShowVO.setContent(comment.getContent());
            wxCommentListShowVO.setPicList(comment.getPicUrls());

            wxCommentListShowVOS.add(wxCommentListShowVO);
        }

        ok.setList(wxCommentListShowVOS);
        return ok;
    }

    @Override
    public WxCommentCountVO countCommentByValueidAndType(String valueId, Integer type) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(Integer.parseInt(valueId)).andTypeEqualTo(type.byteValue());
        Long all = commentMapper.countByExample(commentExample);
        CommentExample.Criteria criteria1 = criteria.andHasPictureEqualTo(true);

        Long hasPic = commentMapper.countByExample(commentExample);
        WxCommentCountVO wxCommentCountVO = new WxCommentCountVO();

        wxCommentCountVO.setAllCount(all.intValue());
        wxCommentCountVO.setHasPicCount(hasPic.intValue());
        return wxCommentCountVO;
    }

    @Override
    public int insertComment(CommmentPostBO commmentPostBO) {

        Comment comment = new Comment();
        comment.setContent(commmentPostBO.getContent());
        comment.setAddTime(new Date());
        comment.setHasPicture(commmentPostBO.getHasPicture());
        comment.setPicUrls(commmentPostBO.getPicUrls());
        comment.setStar(commmentPostBO.getStar());
        Integer userID = ShiroGetSubjectMessageUtil.getUserID();
        comment.setUserId(userID);

        comment.setValueId(Integer.parseInt(commmentPostBO.getValueId()));
        comment.setType(commmentPostBO.getType());

        int i = commentMapper.insertSelective(comment);

        return i;
    }
}
